﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Models.Input;
using DSAManager.Models.Output;
using DSAManager.Utils;

namespace DSAManager
{
    public partial class FrmSalesman : Form
    {
        public FrmSalesman()
        {
            InitializeComponent();
        }

        private FrmSalesmanRecommend frmSalesmanRecommend;

        private string id = "";
        private string qrcodeurl = "";
        bool isEdit = false;
        private void btn_new_Click(object sender, EventArgs e)
        {
            resetInput();
            isEdit = false;
            id = Guid.NewGuid().ToString().Replace("-", "").ToLower();
            var result = Api.CreateByStr(id);
            if (result.data.errcode == 0)
            {
                qrcodeurl = result.data.url;
            }
        }

        void resetInput()
        {
            txt_name.Clear();
            txt_address.Clear();
            txt_phone.Clear();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            //创建显示二维码
            SalesmanInfo info = new SalesmanInfo();
            info.SalesmanName = txt_name.Text;
            info.Mobile = txt_phone.Text;
            info.Address = txt_address.Text;
            info.CreateTime = DateTime.Now;
            info.Id = id;
            info.QrCodeUrl = qrcodeurl;


            if (string.IsNullOrEmpty(info.SalesmanName))
            {

                MsgBox.Alert("姓名不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(info.Mobile))
            {

                MsgBox.Alert("电话不能为空!");
                return;
            }

            var result = Api.CheckSalesmanExist(info);
            if (result.ok)
            {
                MsgBox.Alert("姓名已存在，请重新输入!");
                return;
            }
            if (isEdit)
            {
                result = Api.UpdateSalesmanInfo(info);
                if (result.ok)
                {
                    querySalesmanInfo();
                    MsgBox.Alert("保存成功!");
                }
                else
                {
                    MsgBox.Alert("保存失败!");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(info.Id))
                {
                    info.Id = Guid.NewGuid().ToString().Replace("-", "").ToLower();
                    var temp = Api.CreateByStr(info.Id);
                    if (temp.data.errcode == 0)
                    {
                        info.QrCodeUrl = temp.data.url;
                    }
                }
                result = Api.InsertSalesmanInfo(info);
                if (result.ok)
                {
                    querySalesmanInfo();

                    MsgBox.Alert("保存成功!");
                }
                else
                {
                    MsgBox.Alert("保存失败!");
                }
            }
        }

        private void FrmPhotoSize_Load(object sender, EventArgs e)
        {
            querySalesmanInfo();
        }

        List<SalesmanInfo> salesmanInfo = new List<SalesmanInfo>();
        void querySalesmanInfo()
        {

            BackgroundWorker bk = new BackgroundWorker();
            bk.DoWork += delegate
                             {
                                 salesmanInfo.Clear();
                                 salesmanInfo = Api.QuerySalesmanInfo().data;
                             };
            bk.RunWorkerCompleted += delegate
                                         {
                                             list_size.Items.Clear();
                                             foreach (var info in salesmanInfo)
                                             {
                                                 ListViewItem item = list_size.Items.Add(info.SalesmanName);
                                                 item.SubItems.Add(info.Mobile.ToString());
                                                 item.SubItems.Add(info.CreateTime.ToShortDateString());
                                                 item.SubItems.Add(info.RecommendNum.ToString());
                                                 item.Tag = info;
                                             }
                                             if (list_size.Items.Count > 0)
                                             {
                                                 list_size.Items[0].Selected = true;
                                                 list_size.Items[0].EnsureVisible();
                                                 //list_size.Items[0].
                                             }
                                         };
            bk.RunWorkerAsync();

        }

        private void list_size_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (list_size.SelectedItems.Count <= 0) return;
            var item = list_size.SelectedItems[0];

            var info = item.Tag as SalesmanInfo;
            txt_name.Text = info.SalesmanName;
            txt_phone.Text = info.Mobile;
            txt_address.Text = info.Address;
            id = info.Id.ToString();
            qrcodeurl = info.QrCodeUrl;
            isEdit = true;
            //num_width.Value = Convert.ToDecimal(info.width);
            //num_heigth.Value = Convert.ToDecimal(info.height);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            QRCode qrCode = new QRCode();
            if (cbbSize.SelectedItem != null)
            {
                var size = cbbSize.SelectedItem.ToString().Split(new char[] { ':' })[0];
                var path = qrCode.SaveQRCodeImg(qrcodeurl, IntExtension.Parse(size));
                Process.Start(path);
            }
        }

        void addForm(Form form)
        {
            FrmMain mainform = (FrmMain)this.ParentForm;
            mainform.pan_forms.Controls.Clear();
            form.TopLevel = false;
            mainform.pan_forms.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            form.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (frmSalesmanRecommend == null)
                frmSalesmanRecommend = new FrmSalesmanRecommend();
            addForm(frmSalesmanRecommend);
        }
    }
}
